package com.telecom.at.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.dac.Paging;
import com.telecom.at.db.model.BWList;
import com.telecom.at.db.model.User;

@WebServlet("/front/BWListAdmin")
public class BWListAdmin extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String account = request.getParameter("account");
        if (account == null)
            account = "";
         System.out.println(account);
        String pageNoStr = request.getParameter("pageNo");
        if (pageNoStr == null)
            pageNoStr = "0";
        int pageNo = Integer.parseInt(pageNoStr);

        String pageSizeStr = request.getParameter("pageSize");
        if (pageSizeStr == null)
            pageSizeStr = "10";
        int pageSize = Integer.parseInt(pageSizeStr);

        if (request.getParameter("lastPage") != null)
            pageNo = Integer.MAX_VALUE;
        if (request.getParameter("firstPage") != null)
            pageNo = 0;
        if (request.getParameter("previousPage") != null)
            pageNo--;
        if (request.getParameter("nextPage") != null)
            pageNo++;

        List<BWList> bWList = null;
        User curUser = (User) request.getSession().getAttribute("curUser");
        if (curUser.getId() == 0) {
            bWList = Dac.getInstance().findBWListByAccount(account);
        } else {
            bWList = Dac.getInstance().getBWListByCity(curUser.getCity());
        }
        
        

        Paging<BWList> paging = new Paging<BWList>(pageSize, pageNo, bWList);
        request.setAttribute("paging", paging);
        request.setAttribute("account", account);
        //request.setAttribute("bWList", bWList);
        request.setAttribute("curUser", curUser);
        request.getRequestDispatcher("/WEB-INF/jsp/front/BWListAdmin.jsp").forward(request, response);
    }
}
